package com.healthcloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.healthcloud.constants.InstrumentConsts;
import com.healthcloud.dao.QulitityMapper;
import com.healthcloud.entity.*;
import com.healthcloud.service.BusInstrumentService;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.QulitityService;
import com.healthcloud.utils.CommonMethod;
import com.healthcloud.utils.Constant;
import com.healthcloud.utils.PageParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class QulitityServiceImpl implements QulitityService {

    @Autowired
    private QulitityMapper qulitityMapper;

    @Autowired
    private HlHospitalService hlHospitalService;

    @Autowired
    private BusInstrumentService busInstrumentService;

    @Override
    public List<QulitityEntity> search(QulitityEntity entity, PageParam pageParam) {
        return qulitityMapper.search(entity, pageParam, hlHospitalService.getChildeIds());
    }

    @Override
    public int count(QulitityEntity entity) {
        return qulitityMapper.count(entity, hlHospitalService.getChildeIds());
    }

    @Override
    public List<CheckKBEntity> searchKB(CheckKBEntity entity, PageParam pageParam) {
        return qulitityMapper.searchKB(entity, pageParam);
    }

    @Override
    public CheckKBEntity searchKBById(int id) {
        return qulitityMapper.searchKBById(id);
    }

    @Override
    public int countKB(CheckKBEntity entity) {
        return qulitityMapper.countKB(entity);
    }

    @Override
    public int updateKB(CheckKBEntity entity) {
        return qulitityMapper.updateKB(entity);
    }

    @Override
    public int addKB(CheckKBEntity entity) {

        return qulitityMapper.addKB(entity);
    }

    @Override
    public List<TestDataEntity> record(TestDataEntity entity, PageParam pageParam) {

        return qulitityMapper.record(entity, pageParam);
    }

    @Override
    public int countRecord(TestDataEntity entity) {

        return qulitityMapper.countRecord(entity);
    }

    @Override
    public List<InstrumentInfo> searchDevice(InstrumentInfo entity,
                                             PageParam pageParam) {

        return qulitityMapper.searchDevice(entity, pageParam);
    }

    @Override
    public int countDevice(InstrumentInfo entity) {

        return qulitityMapper.countDevice(entity);
    }

    @Override
    public int createDevice(InstrumentInfo entity) {

        return qulitityMapper.createDevice(entity);
    }

    @Override
    public int updateDevice(InstrumentInfo entity) {

        return qulitityMapper.updateDevice(entity);
    }

    @Override
    public int deleteDevice(int id) {

        return qulitityMapper.deleteDevice(id);
    }

    @Override
    public InstrumentInfo searchDeviceById(int id) {

        return qulitityMapper.searchDeviceById(id);
    }

    @Override
    @Transactional
    public int deletes(int[] ids) {
        int j = 0;
        for (int i = 0; i < ids.length; i++) {
            j = deleteDevice(ids[i]);
        }
        return j;
    }

    @Override
    public List<InstrumentInstallEntity> searchInstall(InstrumentInstallEntity entity,
                                                       PageParam pageParam) {

        return qulitityMapper.searchInstall(entity, pageParam);
    }

    @Override
    public InstrumentInstallEntity searchInstallById(int id) {

        return qulitityMapper.searchInstallById(id);
    }

    @Override
    public List<InstrumentInstallEntity> searchInstallByEntity(
            InstrumentInstallEntity entity) {

        return qulitityMapper.searchInstallByEntity(entity);
    }

    @Override
    public int countInstall(InstrumentInstallEntity entity) {

        return qulitityMapper.countInstall(entity);
    }

    @Override
    public int createInstall(InstrumentInstallEntity entity) {

        return qulitityMapper.createInstall(entity);
    }

    @Override
    public int updateInstall(InstrumentInstallEntity entity) {

        return qulitityMapper.updateInstall(entity);
    }

    @Override
    public int infoInstall(InstrumentInstallEntity entity) {

        return qulitityMapper.infoInstall(entity);
    }

    @Override
    public int deleteInstall(int id) {

        //删除的同时，设备状态更改未投放
        InstrumentInstallEntity entity = qulitityMapper.searchInstallById(id);
        BusInstrumentEntity instrumentEntity = new BusInstrumentEntity();
        instrumentEntity.setInstrumentSN(entity.getMkMedicalInstrumenId());
        instrumentEntity.setInstrumentStatus(InstrumentConsts.INSTRUMENT_STATUS_UNINSTALL);
        busInstrumentService.updateInstr(instrumentEntity);
        return qulitityMapper.deleteInstall(id);
    }

    @Override
    public int unbundlingInstall(List<Integer> ids) {

        for (Integer id : ids) {
            //解除绑定的同时，设备状态更改未投放
            InstrumentInstallEntity entity = qulitityMapper.searchInstallById(id);
            BusInstrumentEntity instrumentEntity = new BusInstrumentEntity();
            instrumentEntity.setInstrumentSN(entity.getMkMedicalInstrumenId());
            instrumentEntity.setInstrumentStatus(InstrumentConsts.INSTRUMENT_STATUS_UNINSTALL);
            busInstrumentService.updateInstr(instrumentEntity);
        }
        return qulitityMapper.unbundlingInstall(ids);
    }

    @Override
    public int deletesInstall(int[] ids) {
        int j = 0;
        for (int i = 0; i < ids.length; i++) {
            j = deleteInstall(ids[i]);
        }
        return j;
    }

    @Override
    public List<VersionEntity> version(VersionEntity entity, PageParam pageParam) {

        return qulitityMapper.version(entity, pageParam);
    }

    @Override
    public int countVersion(VersionEntity entity) {

        return qulitityMapper.countVersion(entity);
    }

    @Override
    public VersionEntity searchVersionById(int id) {

        return qulitityMapper.searchVersionById(id);
    }

    @Override
    public int createVersion(VersionEntity entity) {

        return qulitityMapper.createVersion(entity);
    }

    @Override
    public int updateVersion(VersionEntity entity) {

        return qulitityMapper.updateVersion(entity);
    }

    @Override
    public int updateVersionAddress(VersionEntity entity) {

        return qulitityMapper.updateVersionAddress(entity);
    }

    @Override
    public int deleteVersion(int id) {

        return qulitityMapper.deleteVersion(id);
    }

    @Override
    public List<QualityEntity> quadata(QualityEntity entity, PageParam pageParam) {

        return qulitityMapper.quadata(entity, pageParam);
    }

    @Override
    public int countQuadata(QualityEntity entity) {

        return qulitityMapper.countQuadata(entity);
    }

    @Override
    public List<BaseCodeEntity> searchVersionNum(BaseCodeEntity entity, PageParam pageParam) {

        return qulitityMapper.searchVersionNum(entity, pageParam);
    }

    @Override
    public int countVersionNum(BaseCodeEntity entity) {

        return qulitityMapper.countVersionNum(entity);
    }

    @Override
    public int createVersionNum(BaseCodeEntity entity) {

        return qulitityMapper.createVersionNum(entity);
    }

    @Override
    public int updateVersionNum(BaseCodeEntity entity) {

        return qulitityMapper.updateVersionNum(entity);
    }

    @Override
    public BaseCodeEntity searchVersionNumById(int id) {

        return qulitityMapper.searchVersionNumById(id);
    }

    @Override
    public void deleteBatchByIds(Integer[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return;
        }
        List<Integer> idList = Arrays.asList(ids);
        qulitityMapper.deleteBatchByIds(idList);
    }

}